<template>
  <div class="container-randoms">
    <el-card class="card-box">
      <el-row type="flex" justify="space-between" align="center">
        <el-col class="import-text" :span="8">
          <span style="width: 80px; font-weight: bold; font-size: 16px; color: #606266">关键字</span>
          <el-input v-model="keyword" placeholder="根据编号搜索"></el-input>
        </el-col>
        <el-col>
          <el-row type="flex" justify="end" align="center">
            <el-button @click="clearKeyword">清除</el-button>
            <el-button type="primary" @click="search">搜索</el-button>
          </el-row>
        </el-col>
      </el-row>
      <el-alert :title="`数据一共${counts}条`" type="info"  :closable="false" show-icon class="prompt"> </el-alert>
      <question-table @openDialog="openDialog"  @del="del" v-show="tableData.length > 0" :headList="headList" :tableData="tableData"></question-table>
      <el-row type="flex" justify="end" align="center" class="pagination">
        <el-pagination
          :current-page="page"
          :page-sizes="[20, 30, 50, 100]"
          @current-change="currentChange"
          @size-change="sizeChange"
          :page-size="pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="counts"
        >
        </el-pagination>
      </el-row>
      <question-preview v-if="dialogVisible" :list="list" :dialogVisible.sync="dialogVisible"></question-preview>
    </el-card>
  </div>
</template>

<script>
import { randoms, removeRandoms, detail } from '@/api/hmmm/questions'
import QuestionTable from '../components/questions-table.vue'
import QuestionPreview from '../components/questions-preview.vue'
export default {
  components: { QuestionTable, QuestionPreview },
  data () {
    return {
      keyword: '', // 搜索关键词
      page: 1, // 页码
      pagesize: 20, // 当前页总数
      tableData: [], // 数据列表
      counts: 0, // 数据总数
      headList: [
        { id: 4, label: '录入时间', props: 'addTime' },
        { id: 5, label: '答题时间(s)', props: 'totalSeconds' },
        { id: 6, label: '正确率(%)', props: 'accuracyRate' },
        { id: 7, label: '录入人', props: 'userName' }
      ],
      dialogVisible: false, // 弹框显示隐藏
      list: {}
    }
  },
  created () {
    this.getRandoms()
  },
  methods: {
    // 获取组题列表
    async getRandoms () {
      try {
        const { data } = await randoms({ page: this.page, pagesize: this.pagesize, keyword: this.keyword })
        this.counts = data.counts // 总的数量
        this.tableData = data.items // 主体内容
        // console.log(data)
      } catch (error) {
        console.log(error)
      }
    },
    // 页码切换
    sizeChange (pagesize) {
      this.pagesize = pagesize // 页码数量改变
      this.getRandoms()
    },
    currentChange (page) {
      this.page = page // 页码改变
      this.getRandoms()
    },
    // 搜索
    search () {
      this.getRandoms()
    },
    // 清除keyword
    clearKeyword () {
      this.keyword = ''
    },
    // 删除
    async del (val) {
      if ((this.counts - 1) % this.pagesize === 0 && (this.page > 1)) {
        this.page -= 1
        console.log(1)
      }
      try {
        await this.$alert('确定删除此项数据', {
          confirmButtonText: '确定'
        })
        await removeRandoms({ id: val.id })
        this.getRandoms()
      } catch (error) {
        console.log(error)
      }
    },
    async openDialog (scope) {
      console.log(scope)
      // console.log(data)
      const { data } = await detail({ id: scope.id })
      this.list = data
      this.dialogVisible = true
    }
  }
}
</script>

<style scoped lang="scss">
.container-randoms {
  padding: 15px 10px;
  .card-box {
    padding: 10px;
    .import-text {
      display: flex;
      align-items: center;
      padding-left: 20px;
    }
  }
  .prompt {
    width: 100%;
    margin: 15px 0;
  }
  .pagination {
    padding-top: 30px;
    height: 60px;
  }
}
</style>
